package com.github.permissiondog.cancanpbackend.servlet;

import com.github.permissiondog.cancanpbackend.response.BaseResponse;
import com.github.permissiondog.cancanpbackend.response.ChoicesResponse;
import com.github.permissiondog.cancanpbackend.response.QuestionResponse;
import com.github.permissiondog.cancanpbackend.service.TemplateService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/template/question/choice")
public class QuestionChoicesServlet extends BaseServlet{
    /**
     * 获取审批问题选项
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
        int questionId;
        try {
            questionId = Integer.parseInt(req.getParameter("questionId"));
        } catch (Exception e) {
            json(res, BaseResponse.builder().ok(false).msg("参数错误").build());
            return;
        }
        var question = TemplateService.getInstance().getQuestionById(questionId);
        if (question == null || !question.getType().isChoice()) {
            json(res, BaseResponse.builder().ok(false).msg("题目不存在或类型错误").build());
            return;
        }
        var choices = TemplateService.getInstance().getChoices(questionId);
        json(res, ChoicesResponse.builder().ok(true).choices(choices).build());
    }
}
